﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using WanWebApplication;
using WechatModule.API;
using WechatModule.Tool;
using System.Net;
using System.Net.Sockets;
using RgUtility.Web.WebForm;
using WanWebApplication.Common;


namespace WebApplication1.MemberShip
{
    public partial class command : System.Web.UI.Page
    {
        JScriptValidator validator = new JScriptValidator();
        protected void Page_Load(object sender, EventArgs e)
        {

           
            if (!IsPostBack)
            {

                divMenu.Visible = false;
                string callbackPage = GlobalVar.WechatLoginCallbackPageUrl;
                string idStr = "1";
                string tempStr = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + WechatGlobalVar.AppId + "&redirect_uri=" + callbackPage + "&response_type=code&scope=snsapi_base&state=" + idStr + "#wechat_redirect";
                //aTextHttps.HRef = tempStr;
            }

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string command = txtCommand.Value;
            if (command == "CreateMenu")
            {
                var error = WechatBaseAPI.CreateMenu(null);
                WebHelperForClient.Alert(this, error.errmsg);

            }
            if (command == "TestPort")
            {
                divMenu.Visible = true;

            }
            if (command == "SelectDb")
            {
                Response.Redirect("SelectDb.aspx");
            }
            if (command=="Log")
            {
                Response.Redirect("Log.aspx");
            }

            txtCommand.InnerText = "";
        }
        #region 网络诊断工具

        protected void btnTestPort_Click(object sender, EventArgs e)
        {
            try
            {
                lbError.Text = "";
                lbDemainName.Text = "";
                lbPort.Text = "";
                lbTcp.Text = "";
                lbUdp.Text = "";
                //
                bool isTcpListen = false;
                bool isUdpListen = false;//设定端口状态标识位
                //
                System.Net.IPAddress myIpAddress = Dns.GetHostAddresses(txtDemanName.Text)[0];
                System.Net.IPEndPoint myIpEndPoint = new IPEndPoint(myIpAddress, int.Parse(txtPort.Text));
                //
                System.Net.Sockets.TcpClient tcpClient = new TcpClient();
                try
                {


                    tcpClient.Connect(myIpEndPoint);//对远程计算机的指定端口提出TCP连接请求
                    isTcpListen = true;
                }
                catch
                {
                    isTcpListen = false;
                }
                finally
                {
                    tcpClient.Close();
                }
                //
                System.Net.Sockets.UdpClient udpClient = new UdpClient();
                try
                {
                    udpClient.Connect(myIpEndPoint);//对远程计算机的指定端口提出UDP连接请求
                    isUdpListen = true;
                }
                catch
                {
                    isUdpListen = false;
                }
                finally
                {
                    udpClient.Close();
                }
                //
                if (isTcpListen)
                {
                    lbTcp.Text = "通畅";
                }
                else
                {
                    lbTcp.Text = "关闭";
                }
                if (isUdpListen)
                {
                    lbUdp.Text = "通畅";
                }
                else
                {
                    lbUdp.Text = "关闭";
                }
                lbPort.Text = HttpUtility.HtmlEncode(txtPort.Text);
                lbDemainName.Text = HttpUtility.HtmlEncode(txtDemanName.Text);
            }
            catch (Exception ex)
            {

                lbError.Text = ex.Message;
            }


        }


        protected void btnHiddenTestPort_Click(object sender, EventArgs e)
        {
            divMenu.Visible = false;
        }

        #endregion
    }
}